home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10744 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  52 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!zodiac!szh
  3. From: szh@zcon.com (Syed Zaeem Hosain)
  4. Subject: Re: Q about the float point format......
  5. Message-ID: <1996Mar19.180521.24842@zcon.com>
  6. Sender: szh@zcon.com (Syed Zaeem Hosain)
  7. Nntp-Posting-Host: zodiac
  8. Reply-To: szh@zcon.com
  9. Organization: Z Consulting Group
  10. References: <s3032089.15.314E68DD@sparc13.ncu.edu.tw>
  11. Date: Tue, 19 Mar 1996 18:05:21 GMT
  12.  
  13. In article <s3032089.15.314E68DD@sparc13.ncu.edu.tw>, s3032089@sparc13.ncu.edu.tw (Alexander PeaceLand) writes:
  14. >
  15. >   Could I dynamicly set the digits after the float POINT?
  16. >
  17. >   In other word... I use printf to print the float point number
  18. >                    like 123.456789 but in the other time i only 
  19. >                    want to print 123.456 or 123.4 
  20. >                    How shall I do? Thanks! :)
  21.  
  22. This information should be available in your compiler and run-time
  23. library reference manuals - look for printf format string descriptions.
  24. Basically, you need to put some information in the format strings. See
  25. the following as an example:
  26.  
  27. zodiac{137}szh: cat foo.c
  28. #include <stdio.h>
  29.  
  30. int main ( void )
  31. {
  32.   double d = 123.456789;
  33.  
  34.   printf ( "%.6f\n" , d );
  35.   printf ( "%.3f\n" , d );
  36.   printf ( "%.1f\n" , d );
  37.   return ( 0 );
  38. }
  39. zodiac{138}szh: gcc -o foo foo.c
  40. zodiac{139}szh: foo
  41. 123.456789
  42. 123.457
  43. 123.5
  44. zodiac{140}szh: 
  45.  
  46.  
  47. -- 
  48. -------------------------------------------------------------------------
  49. | Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |
  50. | Z Consulting Group        San Jose, CA 95161             szh@zcon.com |
  51. -------------------------------------------------------------------------
  52.